package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin/common")
@Api(tags = "文件上传接口")
public class CommonController {

    @Autowired
   private AliOssUtil aliOssUtil ;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传");

        try {
            //获取原始文件名
            String origina = file.getOriginalFilename().toString();

            //获取文件后缀名
            String extention = origina.substring(origina.lastIndexOf("."));

            //随机生成UUID的文件名（避免上传文件名字重复）
            String objectname = UUID.randomUUID() + extention;

            String url = aliOssUtil.upload(file.getBytes(), objectname);

            return Result.success(url);

        } catch (IOException e) {
            log.info("文件上传失败",e);
        }
        return null;

    }

}
